package com.tencent.qcloud.tuikit.tuichat.order;


import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cqyc.network.Response;
import com.cqyc.network.getid.GetMyInformationBox;
import com.cqyc.network.http.HttpUtil;
import com.cqyc.network.model.OrderInfo;
import com.cqyc.network.utils.ToastUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.tencent.qcloud.tuikit.tuichat.R;
import com.tencent.qcloud.tuikit.tuichat.util.DeviceUtil;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;

public class OrderListPopup implements View.OnClickListener {
    private PopupWindow popupWindow;
    private Context context;
    private View emptyView;
    private SmartRefreshLayout refreshLayout;
    private String uId;
    private int page;
    private AdapterOrderPopup adapter;

    public OrderListPopup(Context context) {
        this.context = context;
        popupWindow = new PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, (int) (DeviceUtil.getScreenSize()[1] * 0.65f));
        popupWindow.setAnimationStyle(R.style.BottomDialog_Animation);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        View view = LayoutInflater.from(context).inflate(R.layout.popup_order_list, null);
        popupWindow.setContentView(view);
        refreshLayout = view.findViewById(R.id.smart_layout);
        emptyView = view.findViewById(R.id.empty_view);
        TextView textView = emptyView.findViewById(R.id.empty_text);
        textView.setText("暂无任何订单信息哦");
        view.findViewById(R.id.fragment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });

        refreshLayout.setEnableRefresh(false);
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getNextPage();
            }
        });

        view.findViewById(R.id.close_btn).setOnClickListener(this);
        RecyclerView recyclerView = view.findViewById(R.id.recycler);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new AdapterOrderPopup(context);
        recyclerView.setAdapter(adapter);
    }

    public void setUid(String uid) {
        this.uId = uid;
    }

    public void setView(List<OrderInfo.ListBean> list) {
        if (list == null || list.size() == 0) {
            emptyView.setVisibility(View.VISIBLE);
        } else {
            emptyView.setVisibility(View.GONE);
        }
        adapter.setData(list);
    }

    //获取下一页数据
    private void getNextPage() {
        Call<Response<OrderInfo>> call;
        if (GetMyInformationBox.INSTANCE.isBuyer()) {
            call = HttpUtil.api.tradeListBuy(uId, page, "20");
        } else {
            call = HttpUtil.api.tradeList(uId, page, "20");
        }
        call.enqueue(new Callback<Response<OrderInfo>>() {
            @Override
            public void onResponse(Call<Response<OrderInfo>> call, retrofit2.Response<Response<OrderInfo>> response) {
                refreshLayout.finishLoadMore();
                if (response.isSuccessful()) {
                    page++;
                    Response<OrderInfo> result = response.body();
                    if (result != null) {
                        if (result.getCode() == 2000) {
                            if (result.getData() == null || result.getData().getList() == null || result.getData().getList().size() <= 0) {
                                //没有更多数据了
                                refreshLayout.setEnableLoadMore(false);
                            } else {
                                adapter.addData(result.getData().getList());
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<Response<OrderInfo>> call, Throwable t) {
                ToastUtils.showLong(context, "获取订单异常请重试!");
            }
        });
    }

    public void show(View view) {
        page = 2;
        refreshLayout.setEnableLoadMore(true);
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.close_btn) {
            popupWindow.dismiss();
        }
    }


    public void setOrderClickListener(OrderClickListener orderClickListener) {
        adapter.setOrderClickListener(orderClickListener);

    }

    public static interface OrderClickListener {
        void onClick(int position, OrderInfo.ListBean data);
    }

}
